package user

import (
	"context"
	"git.myscrm.cn/golang/stark/v4"
	"gorm.io/gorm"
	"strings"
	"time"
)

type CommonUserLoginRecodeRepository struct {
}

func NewCommonUserLoginRecodeRepository() CommonUserLoginRecodeRepoIface {
	return &CommonUserLoginRecodeRepository{}
}

//获取详情
func (r *CommonUserLoginRecodeRepository) GetInfo(ctx context.Context, condition map[string]interface{}) (*CommonUserLoginRecord, error) {
	db := stark.MySQLConn.Get(ctx)
	commonUserLoginRecode := &CommonUserLoginRecord{}
	err := db.Table(commonUserLoginRecode.TableName()).Where(condition).First(&commonUserLoginRecode).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return commonUserLoginRecode, nil
}

// 获取有效登录记录
func (r *CommonUserLoginRecodeRepository) GetAvailableLoginRecode(ctx context.Context, token string) (*CommonUserLoginRecord, error) {
	db := stark.MySQLConn.Get(ctx)
	commonUserLoginRecode := &CommonUserLoginRecord{}

	err := db.Table(commonUserLoginRecode.TableName()).Where("token =? and is_deleted = 0", token).First(&commonUserLoginRecode).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return commonUserLoginRecode, nil
}

//获取列表数据
func (r *CommonUserLoginRecodeRepository) GetList(ctx context.Context, condition map[string]interface{}, page, pagesize int, orderBy string) (int64, []*CommonUserLoginRecord, error) {
	db := stark.MySQLConn.Get(ctx)

	commonUserLoginRecodeList := []*CommonUserLoginRecord{}
	var count int64 = 0
	if page < 1 {
		page = 1
	}
	if pagesize < 1 {
		pagesize = 10
	}
	offset := (page - 1) * pagesize

	query := db.Where(condition)
	err := query.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}
	query = query.Offset(offset).Limit(pagesize).
		Find(&commonUserLoginRecodeList)
	if orderBy != "" {
		query = query.Order(orderBy)
	}
	err = query.Error
	if err != nil {
		return 0, nil, err
	}
	return count, commonUserLoginRecodeList, nil
}

//更新数据
func (r *CommonUserLoginRecodeRepository) Update(ctx context.Context, data, condition map[string]interface{}) (bool, error) {
	db := stark.MySQLConn.Get(ctx)
	err := db.Model(&CommonUserLoginRecord{}).Where(condition).Updates(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

//插入数据
func (r *CommonUserLoginRecodeRepository) Insert(ctx context.Context, commonUserLoginRecode *CommonUserLoginRecord) (bool, error) {
	db := stark.MySQLConn.Get(ctx)
	err := db.Save(commonUserLoginRecode).Error
	if err != nil {
		return false, err
	}

	return true, nil
}

//逻辑删除数据
func (r *CommonUserLoginRecodeRepository) Delete(ctx context.Context, condition map[string]interface{}) (bool, error) {
	//todo 补充逻辑
	return false, nil
}

func (r *CommonUserLoginRecodeRepository) Logout(ctx context.Context, globalUserId string, logoutType string) (bool, error) {
	db := stark.MySQLConn.Get(ctx)
	loginType := ""
	if logoutType != "all" {
		loginType = logoutType
	}
	db = db.Model(&CommonUserLoginRecord{}).Where("common_user_data_uid = ?", globalUserId)
	if loginType != "" {
		loginTypeSlice := strings.Split(logoutType, ",")
		db = db.Where("login_type in ?", loginTypeSlice)
	}
	dd, _ := time.ParseDuration("-24h")
	dateTime := time.Now().Add(dd)
	err := db.Where("is_deleted = 0 and created_time > ?", dateTime).Limit(1000).Updates(map[string]interface{}{"is_deleted":1}).Error
	return true, err
}

func (r *CommonUserLoginRecodeRepository) LogoutByToken(ctx context.Context, token string) error {
	db := stark.MySQLConn.Get(ctx)
	db = db.Model(&CommonUserLoginRecord{}).Where("token = ?", token)
	err := db.Updates(map[string]interface{}{"is_deleted":1}).Error
	return err
}